package com.xiao.examples;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.BatteryManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

import java.util.Locale;

/**
 * 系统工具类
 * Created by 程龙 on 2018-09-12.
 */
public class SystemUtil {
    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    public static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }
    /**
     * 获取当前系统上的语言列表(Locale列表)
     *
     * @return 语言列表
     */
    public static Locale[] getSystemLanguageList() {
        return Locale.getAvailableLocales();
    }
    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }
    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }
    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // 获取 IMEI（从 Android 10 开始，设备可能有多个 IMEI）
            String imei = tm.getImei();
        } else {
            // Android 10 之前使用 getDeviceId()
            String imei = tm.getDeviceId();
        }
        return null;
    }
    /**
     * 获取手机号码(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getPhoneNo(Context ctx, MainActivity activity) {
        TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            String simNumber = telephonyManager.getLine1Number(); // 获取 SIM 卡号码
            return simNumber;
        } else {
            // 需要请求权限
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
            return "Unknown";
        }
    }
    /**
     * 获取手机号码(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getSimOperatorName(Context ctx, MainActivity activity) {
        TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            String simNumber = telephonyManager.getSimOperatorName(); // 获取 SIM 卡号码
            return simNumber;
        } else {
            // 需要请求权限
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
            return "Unknown";
        }
    }

    /**
     * 获取手机剩余电量
     *
     * @return 手机电量
     */
    public static int getBattery(Context ctx) {
        BatteryManager batteryManager = (BatteryManager) ctx.getSystemService(Context.BATTERY_SERVICE);
        int batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); // 获取电池电量百分比
        return batteryLevel;
    }

    /**
     * 获取手机定位
     * 需要 implementation 'com.google.android.gms:play-services-location:18.0.0'
     * @return 回调，经纬度
     *
     * 如果需要实时更新位置，可以用如下代码进行定位更新
     *
     * LocationRequest locationRequest = LocationRequest.create();
     * locationRequest.setInterval(10000); // 设置定位间隔为10秒
     * locationRequest.setFastestInterval(5000); // 设置最快定位间隔为5秒
     * locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
     *
     * fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
     *
     */
    public static void getLocation(Context ctx, MainActivity activity) {
        FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(activity);

        if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }

        // 获取位置信息
        fusedLocationClient.getLastLocation().addOnSuccessListener(activity, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    // 使用location对象获得经纬度信息
                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();
                    // 在此处理位置数据
                    Log.i("定位", String.format("经度：%.2f 纬度：%.2f", longitude, latitude));
                }
            }
        });
    }


    public static void showSystemParameter(Context ctx, MainActivity activity) {
        Log.i("", "*******************************************");
        String TAG = "系统参数：";
        Log.i(TAG, "手机厂商：" + SystemUtil.getDeviceBrand());
        Log.i(TAG, "手机型号：" + SystemUtil.getSystemModel());
        Log.i(TAG, "手机当前系统语言：" + SystemUtil.getSystemLanguage());
        Log.i(TAG, "Android系统版本号：" + SystemUtil.getSystemVersion());
//        Log.i(TAG, "手机IMEI：" + SystemUtil.getIMEI(ctx));
        Log.i(TAG, "手机号码：" + SystemUtil.getPhoneNo(ctx, activity));
        Log.i(TAG, "手机卡运营商：" + SystemUtil.getSimOperatorName(ctx, activity));
        Log.i(TAG, "手机电量(%)：" + SystemUtil.getBattery(ctx));
        SystemUtil.getLocation(ctx, activity);
        Log.i("", "*******************************************");
    }
}
